//
//  AddEventViewController.m
//  Remind
//
//  Created by zhufeng on 2017/10/23.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "AddEventViewController.h"
#import "DataCach.h"
#import "PickDateTimeCell.h"
#define ROWH 60
@interface AddEventViewController ()

@end

@implementation AddEventViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveData)];
    [self initializeForm];
    
}


- (void)initializeForm{
    XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:@"添加事件表单"];
    
    XLFormSectionDescriptor *section = [XLFormSectionDescriptor formSection];
    [form addFormSection:section];
    
    XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:@"event" rowType:XLFormRowDescriptorTypeText title:@"事件"];
    [row.cellConfig setObject:@"输入事件名称" forKey:@"textField.placeholder"];
    row.height = ROWH;
    [section addFormRow:row];
  
    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"date" rowType:XLFormRowDescriptorTypePickDateAndTime];
    [row.cellConfig setObject:@"日期" forKey:@"titleLabel.text"];
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd-HH:mm"];
    NSString *dateString = [formatter stringFromDate:date];
    row.value = dateString;
    row.height = ROWH;
    [section addFormRow:row];
    
    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"class" rowType:XLFormRowDescriptorTypeSelectorAlertView title:@"类型"];
    row.selectorOptions = [DataCach GetTheSortedArray];
    row.value = [DataCach GetTheSortedArray].firstObject;
    row.height = ROWH;
    [section addFormRow:row];
    
    self.form = form;
}

- (void)saveData{
    
    NSDictionary *data = self.formValues;
    NSLog(@"--_____-->数据%@",data);
    for (NSString *key in data.allKeys) {
        if([[data objectForKey:key] isKindOfClass:[NSNull class]]){
            NSLog(@"一项数据为空");
            return ;
        }
    }
    
    NSString *dateStr = [NSString stringWithFormat:@"%@",[data objectForKey:@"date"]];
    NSString *eventStr = [NSString stringWithFormat:@"%@",[data objectForKey:@"event"]];
    NSString *className = [NSString stringWithFormat:@"%@",[data objectForKey:@"class"]];

    NSMutableDictionary *contentDict = [NSMutableDictionary dictionary];
    [contentDict setObject:eventStr forKey:@"event"];
    [contentDict setObject:dateStr forKey:@"date"];

    NSString *key = [dateStr substringToIndex:16];
    key = [key stringByAppendingString:[@"-" stringByAppendingString:eventStr]];
    //NSLog(@"%@",key);
    
    NSDictionary *dict = @{
                           key:contentDict
                           };
    BOOL back = [DataCach setObject:dict forClassName:className IfTheDataIsNullInitialized:[NSMutableDictionary class] ReplaceData:NO];
    [DataCach getValueByClassName:className];
    if(back) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reFreshTableView" object:className];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else return;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
